package com.test.cyclicBarrierTest;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

/**
 *
 * 验证CyclicBarrier的屏障。 两个线程两个线程一起跑
 *
 *
 *
 * @author:wesley
 */
public class CyclicBarrierRunService {

    private CyclicBarrier cyclicBarrier;

    public CyclicBarrierRunService(CyclicBarrier cyclicBarrier) {
        this.cyclicBarrier = cyclicBarrier;
    }

    public void doRun(){
        long sleepV = (long) (Math.random() * 10000);

        try {
            Thread.sleep(sleepV);
            System.out.println(Thread.currentThread().getName()+"开始第1阶段。。。");
            cyclicBarrier.await();
            System.out.println(Thread.currentThread().getName()+"结束第1阶段。。。");


            sleepV = (long) (Math.random() * 10000);
            System.out.println(Thread.currentThread().getName()+"开始第2阶段。。。");
            cyclicBarrier.await();
            System.out.println(Thread.currentThread().getName()+"结束第2阶段。。。");



        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (BrokenBarrierException e) {
            e.printStackTrace();
        }


    }


}
